package login;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless(name = "LoginBean")
public class LoginBean implements Login{
	
	private boolean loginOk;
	private String userName;
	private String password;
	
	@PersistenceContext(unitName = "sqlPU")
	private EntityManager em;

	public boolean isLoginOk() {
		return loginOk;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String validateLogin() {
		if (userName != null && password != null && !userName.equalsIgnoreCase(password)) {

			Query qm = em.createQuery("FROM EntidadeFuncionario WHERE email = :email AND senha = :senha");
			qm.setParameter("email", userName);
			qm.setParameter("senha", password);
			
			if(qm.getResultList() != null)
			{
				loginOk = true;
				return "secpage";
			}
			else
			{
				return "login";
			}
		} 
		else
			return "login";
	}
}
